#include "TableWidget.h"
#include <algorithm>
#include<iostream>

TableWidget::TableWidget(int x, int y, int width, int height, int visibleRowCount)
    : x(x), y(y), width(width), height(height), visibleRowCount(visibleRowCount) {
    data = { {} };
    rowHeight = height / visibleRowCount;
    selectedRow = -1;
    scrollOffset = 0;
    scrollbarWidth = 20;
    handleHeight = 30;
    handleY = 0;
}



int TableWidget::getSelectedRow() const {
    return selectedRow;
}

//void TableWidget::getSelectedRows(std::vector<int>& a) const {
//    a=selectedRows;
//}

std::wstring TableWidget::GetRowData(int row,int col)
{
    return data[row][col];
}

std::wstring TableWidget::getSelectedInfo(int col) const {
    return data[selectedRow][col];
}

void TableWidget::setData(const std::vector<std::vector<std::wstring>>& newData) {
    data = newData;
    //for (int i = 0; i < data.size(); ++i) {
    //    for (int j = 0; j < data[i].size(); ++j) {
    //        std::wcout << data[i][j] << " ";
    //    }
    //    std::wcout << "\n";
    //}

    calculateColumnWidths();
}

// 计算每列的宽度
void TableWidget::calculateColumnWidths() {
    columnWidths.clear();
    if (!data.empty()) {
        columnWidths.resize(data[0].size(), 0);
        for (const auto& row : data) {
            for (size_t j = 0; j < row.size(); ++j) {
                int width = textwidth(row[j].c_str());
                if (width > columnWidths[j]) {    // 每列的最大宽度（即每列中字符最大长度）
                    columnWidths[j] = width;
                }
            }
        }
    }
    // 根据每列最大宽度计算该列长度
    int sumWidth = 0;
    for (auto it : columnWidths)
        sumWidth += it;
    for (auto& it : columnWidths)
        it = (float)it / sumWidth * width;
}

void TableWidget::scrollUp() {
    if (scrollOffset > 0) {
        scrollOffset--;
    }
    if (scrollOffset < 0) {
        scrollOffset = 0;
    }
}

void TableWidget::scrollDown() {
    int maxScrollOffset = data.size() - visibleRowCount;
    if (scrollOffset < maxScrollOffset) {
        scrollOffset++;
    }
}

void TableWidget::scroll(int mouseX, int mouseY, int wheel) {
    if (mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height) {
        if (wheel > 0) {
            scrollUp();
        }
        else if (wheel < 0) {
            scrollDown();
        }
    }
}

int TableWidget::handleMouseClick(int mouseX, int mouseY) {
    if (mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height) {
        int clickedRow = (mouseY - y) / rowHeight + scrollOffset;

        if (clickedRow >= 0 && clickedRow < static_cast<int>(data.size())) {
            //if (style == 1) {
            //    auto it = std::find(selectedRows.begin(), selectedRows.end(), clickedRow);
            //    if (it != selectedRows.end()) {
            //        // 如果已选中，取消选择
            //        selectedRows.erase(it);
            //    }
            //    else {
            //        // 如果未选中，添加到选中列表
            //        selectedRows.push_back(clickedRow);
            //    }
            //}
            //else if(style==0) 
            selectedRow = clickedRow;
        }
        else {
            selectedRow = -1;
        }
    }
    return selectedRow;
}

void TableWidget::draw() {
    setbkmode(1);
    // 绘制表格区域
    setfillcolor(WHITE);
    solidrectangle(x, y, x + width, y + height);
    setlinecolor(BLACK);
    settextstyle(12, 0, _T("宋体"));

    // 计算需要绘制的行数
    int rowCount = min(visibleRowCount, static_cast<int>(data.size()));

    settextcolor(BLACK);
    // 绘制表头
    int headerY = y;
    int columnX = x;
    for (int j = 0; j < data[0].size(); ++j) {
        int columnWidth = columnWidths[j];
        rectangle(columnX, headerY, columnX + columnWidth, headerY + rowHeight);
        int textX = columnX + (columnWidth - textwidth(data[0][j].c_str())) / 2;
        int textY = headerY + (rowHeight - textheight(_T("宋体"))) / 2;
        outtextxy(textX, textY, data[0][j].c_str());
        columnX += columnWidth;
    }

    // 绘制表格内容
    for (int i = 1; i < rowCount; ++i) {
        int rowY = y + i * rowHeight;
        int dataIndex = i + scrollOffset;
        columnX = x;
        for (int j = 0; j < data[dataIndex].size(); ++j) {
            int columnWidth = columnWidths[j];
            bool isSelectedRow=false;
            /*if (selectedRows.size()>0) {
                isSelectedRow = std::find(selectedRows.begin(), selectedRows.end(), dataIndex) != selectedRows.end();
            }
            else {*/
                isSelectedRow=(dataIndex == selectedRow);
            //}

            if (isSelectedRow) {
                setfillcolor(LIGHTBLUE);
                settextcolor(RED);
            }
            else {
                setfillcolor(WHITE);
                settextcolor(BLACK);
            }
            fillrectangle(columnX, rowY, columnX + columnWidth, rowY + rowHeight);

            int textX = columnX + (columnWidth - textwidth(data[dataIndex][j].c_str())) / 2;
            int textY = rowY + (rowHeight - textheight(_T("宋体"))) / 2;
            outtextxy(textX, textY, data[dataIndex][j].c_str());
            columnX += columnWidth;
            settextcolor(BLACK);
        }
    }
    // 绘制滚动条背景
    int scrollbarX = x + width;
    setfillcolor(LIGHTGRAY);
    solidrectangle(scrollbarX, y, scrollbarX + scrollbarWidth, y + height);

    // 计算滑块位置和大小
    int handleX = scrollbarX;
    int handleWidth = scrollbarWidth;
    int maxHandleY = height - handleHeight;
    handleY = maxHandleY * double(scrollOffset) / (data.size() - visibleRowCount);

    // 绘制滑块
    setfillcolor(DARKGRAY);
    solidrectangle(handleX, y + handleY, handleX + handleWidth, y + handleY + handleHeight);
}